import FindXY
import FindFace
import GetScreen
import GetTxt
import random
import cv2
import pyautogui
import pyperclip

if __name__ == '__main__':


    while True:
        # 等待1秒
        cv2.waitKey(3000)

        # 获取截图
        tempScreenName = "temp/screen" + str(random.randint(1, 1000000)) + ".png"
        GetScreen.capture_screen_and_save(tempScreenName)
        img = cv2.imread(tempScreenName)

        # 查找人脸
        face_img = FindFace.detect_faces(tempScreenName)
        if face_img is not None and len(face_img) > 0:
            for (x, y, w, h) in face_img:
                # 展示人脸
                cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)

                # 点击人脸坐标
                pyautogui.moveTo(x + w // 2, y + h // 2, duration=3)  # duration 是移动所需时间(秒)
                pyautogui.click(x + w // 2, y + h // 2)

                # 等待1~3秒
                cv2.waitKey(random.randint(1000, 3000))

                # 截取下一张截图
                tempScreenName1 = "temp/screen" + str(random.randint(1, 1000000)) + ".png"
                GetScreen.capture_screen_and_save(tempScreenName1)
                img1 = cv2.imread(tempScreenName1)

                # 查找是否有点赞
                top_left, bottom_right, center, max_val = FindXY.find_template_in_image("img/not nice.png",
                                                                                        tempScreenName1)
                if max_val > 0.9:
                    print("未点赞")

                    # 等待5
                    cv2.waitKey(random.randint(1000, 3000))

                    # 点击点赞坐标
                    pyautogui.moveTo(center[0], center[1], duration=0.4)  # duration 是移动所需时间(秒)
                    pyautogui.click(center[0], center[1])

                    # 等待1~3秒
                    cv2.waitKey(random.randint(1000, 3000))

                    print("点赞完成")

                    # 往左移动 开始评论
                    pyautogui.moveTo(center[0] - 100, center[1], duration=0.4)  # duration 是移动所需时间(秒)
                    pyautogui.click(center[0] - 100, center[1])

                    # 输入评论
                    comment = GetTxt.random_compliment("1")
                    pyperclip.copy(comment)  # 使用 pyperclip 操作剪贴板
                    pyautogui.hotkey('ctrl', 'v')  # Windows/Linux
                    pyautogui.press('enter')  # 输入完后按下回车

                    print("评论完成")

                    # 等待1~3秒
                    cv2.waitKey(random.randint(1000, 3000))

                    # 点击关注坐标
                    top_left1, bottom_right1, center1, max_val1 = FindXY.find_template_in_image("img/follow.png",
                                                                                                tempScreenName1)

                    if max_val1 > 0.9:
                        print("开始关注")

                        # 点击关注坐标
                        pyautogui.moveTo(center1[0], center1[1], duration=0.4)  # duration 是移动所需时间(秒)
                        pyautogui.click(center1[0], center1[1])

                        # 等待1~3秒
                        cv2.waitKey(random.randint(1000, 3000))

                        print("关注完成")

                else:
                    print("已点赞")

                #  退出
                pyautogui.moveTo(150, 500, duration=0.4)  # duration 是移动所需时间(秒)
                pyautogui.click(150, 500)

                # 等待1~3秒
                cv2.waitKey(random.randint(1000, 3000))


        else:
            print("未找到人脸")

        # 滚动鼠标滚轮
        pyautogui.moveTo(300, 300, duration=0.4)  # duration 是移动所需时间(秒)
        pyautogui.scroll(-200)
        cv2.waitKey(random.randint(1000, 3000))
        pyautogui.scroll(-200)
        cv2.waitKey(random.randint(1000, 3000))
        pyautogui.scroll(-200)